package com.example.notificationdemo;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;

public class MainActivity extends Activity implements OnClickListener {
    /*Invalid notifacation (no valid small icon):Notification(channel=null pri=0 contentView=null
     vibrate=null sound=null defaults=0x0 flag=0x1 color=0x00000000 vis=PRIVATE)*/
    private Button sendNotice_button;
    private Button button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sendNotice_button=(Button)findViewById(R.id.send_notice);
        sendNotice_button.setOnClickListener(this);
    }
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.send_notice:
                NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);//NotificationManager实例对通知进行管理
                //Notification notification=new Notification(R.mipmap.ic_launcher,"通知",
                //        System.currentTimeMillis());//创建Notification对象
                //notification.setLatestEventInfo(this, "通知标题", "通知内容",null);

                //Uri soundUri=Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg"));
                //notification.sound=soundUri;
                Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.baidu.com"));
                PendingIntent pi=PendingIntent.getActivities(MainActivity.this,0,
                        new Intent[]{intent},0);
                Notification.Builder builder=new Notification.Builder(this);
                builder.setContentTitle("通知标题");
                builder.setContentText("通知内容:收到了一个广播！");
                builder.setContentIntent(pi);
                Notification notification=builder.build();

                notification.ledARGB=Color.GREEN;//控制通知的led灯颜色
                notification.ledOnMS=1000;//通知灯的显示时间
                notification.ledOffMS=1000;
                notification.flags=Notification.FLAG_SHOW_LIGHTS;
                manager.notify(1,notification);//调用NotificationManager的notify方法使通知显示
                break;
            default:
                break;
        }
        return;
    }
}
